package edu.chl.dat255.bluebanana.logic.impl;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import edu.chl.dat255.bluebanana.logic.ISound;

public class APSound implements ISound {
	private static ISound instance = new APSound();
	private static Context staticContext;
	private static AudioManager audioManager;
	private static int streamMaxVolume;
	
	private APSound() {
		// Singleton
	}

	public static ISound getInstance(Context context) {
		staticContext = context;
		audioManager =	(AudioManager)staticContext.getSystemService(Context.AUDIO_SERVICE);
		streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING); 
		return instance;
	}
//
//	@Override
//	public void setRingerMode(int ringerMode) {
//		audioManager.setRingerMode(ringerMode);
//	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public void setSilentMode(boolean isSilentMode) {
		//Ringtone will be silent and will not vibrate
		if(isSilentMode) {	
			audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		} else {
			audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		}
		
		Intent intent = new Intent(AudioManager.RINGER_MODE_CHANGED_ACTION);
        staticContext.sendBroadcast(intent);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setVibrate(boolean isVibrate) {
		if (isVibrate) {
			audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
			audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
		} else {
			audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
			audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
		}
		
		Intent intent = new Intent(AudioManager.VIBRATE_SETTING_CHANGED_ACTION);
        staticContext.sendBroadcast(intent);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setRingVolume(int volume) {
		if (volume > streamMaxVolume) {
			volume = streamMaxVolume;
		} else if (volume < 0) {
			volume = 0;
		}
		audioManager.setStreamVolume(AudioManager.STREAM_RING, volume, 
				AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public void setNotificationVolume(int volume) {
		if (volume > streamMaxVolume) {
			volume = streamMaxVolume;
		} else if (volume < 0) {
			volume = 0;
		}
		audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, volume, 
				AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
		
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setRingtone(String ringtonePath) {
		Uri ringtoneUri = Uri.parse(ringtonePath);
		RingtoneManager.setActualDefaultRingtoneUri(staticContext,
				RingtoneManager.TYPE_RINGTONE, ringtoneUri);
	}

	@Override
	/**
	 * @inheritDoc
	 */
	public void setNotificationRingtone(String notificationPath) {
		Uri notificationUri = Uri.parse(notificationPath);
		RingtoneManager.setActualDefaultRingtoneUri(staticContext,
				RingtoneManager.TYPE_NOTIFICATION, notificationUri);

	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isSilent() {
		return audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public boolean isVibrate() {
		return audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ON 
		 && audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION) == AudioManager.VIBRATE_SETTING_ON;
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public int getRingVolume() {
		return audioManager.getStreamVolume(AudioManager.STREAM_RING);
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public int getNotificationVolume() {
		return audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public String getRingtone() {
		return RingtoneManager.getActualDefaultRingtoneUri(
				staticContext, RingtoneManager.TYPE_RINGTONE).toString();
	}
	
	@Override
	/**
	 * @inheritDoc
	 */
	public String getNotificationTone() {
		return RingtoneManager.getActualDefaultRingtoneUri(
				staticContext, RingtoneManager.TYPE_NOTIFICATION).toString();
	}
}


